var express = require('express');
var router = express.Router();
const  User =require('../models/user_bean')
const userController=require('../controllers/user_controller')
/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});


//登录请求

router.post("/login", (req, res) => {
    console.log(req.body.username, req.body.password);

    userController.doLogin(
        new User.User(req.body.username, req.body.password),
        (ret) => {
            // res.cookie("username", req.body.username);
          // req.session.username = req.body.username;
            res.json(ret);
        },
        (err) => res.json(err)
    );
});
    

//添加
router.post("/add", (req, res) => {
  userController.doregister(
        new User.add(req.body.id, req.body.password,req.body.username, req.body.sex,req.body.age, req.body.scope,req.body.address),
        (ret) => res.json(ret),
        (err) => res.json(err)
    );
});

//修改
router.put('/update', (req, res) => {
      console.log(req.body.id, req.body.username, req.body.password);

  userController.doupdate(
    new User.add(req.body.id, req.body.password,req.body.username, req.body.sex,req.body.age, req.body.scope,req.body.address),
        (ret) => res.json(ret),
        (err) => res.json(err)
    );
})


//查询
router.post('/select', (req, res) => {
  console.log("ssadas",req.body.id,req.body.username, req.body.password);
  userController.doselect(
        new User.update(req.body.id,req.body.username, req.body.password),
        (ret) => res.json(ret),
        (err) => res.json(err)
    );
})

//删除
router.delete('/delete', (req, res) => {
  console.log("开始",req.body.ids);
  userController.dodelete(
        new User.update(req.body.ids),
        (ret) => res.json(ret),
        (err) => res.json(err)
    );
})


//查询全部用户
router.get('/all', (req, res) => {
  userController.doall(
        null,
        (ret) => res.json(ret),
        (err) => res.json(err)
    );
})
    



module.exports = router;
